package com.yitiao.data_algorithms.leetcode.editor.cn;

import com.yitiao.data_algorithms.leetcode.editor.cn.base.ListNode;
import com.yitiao.data_algorithms.leetcode.editor.cn.base.TreeNode;

import java.util.ArrayList;
import java.util.List;

class UnivaluedBinaryTree {
    // 2022-08-15 11:44:40
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode() {}
     * TreeNode(int val) { this.val = val; }
     * TreeNode(int val, TreeNode left, TreeNode right) {
     * this.val = val;
     * this.left = left;
     * this.right = right;
     * }
     * }
     */
    class Solution {
        boolean ans = true;
        List<Integer> list = new ArrayList();
        public boolean isUnivalTree(TreeNode root) {
            if (root == null) return true;

            list.add(root.val);
            dfs(root);
            return ans;
        }

        private void dfs(TreeNode root) {
            if (root == null) return;

            if (!list.contains(root.val)) {
                ans = false;
            }
            dfs(root.left);
            dfs(root.right);

        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}